package com.cwz.taculture.controller.index;

import com.cwz.taculture.pojo.ShoppingTrolleyPojo;
import com.cwz.taculture.service.index.ShoppingCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 关于首页购物车的操作
 */

//@Controller
    @RestController
    @CrossOrigin
public class ShoppingCarController {
    @Autowired
    private ShoppingCarService _shopping_car_service;
    /**
     * 查询 ShoppingTrolley 购物车中的总数据   根据用户名进行查询总行数
     */
    @RequestMapping("FindShoppingTrolleyByRidCount/{userId}")
    public int FindShoppingTrolleyByRidCount(HttpServletRequest request,@PathVariable("userId") Integer userId){
        if (userId==null){
            userId=0;
        }
        Integer count = _shopping_car_service.findShoppingTrolleyByRidCount(userId);
        return count;
    }
//    查询购物车中的画是否有重复
    @RequestMapping("FindShoppingTrolleyCount/{commodityName}/{uid}")
    public Integer FindShoppingTrolleyCount(@PathVariable("commodityName") String commodityName, HttpServletRequest request,@PathVariable("uid") Integer uid){
        Integer createCommodityId = _shopping_car_service.findCommodityById(commodityName);
        Integer count = _shopping_car_service.findShoppingTrolleyCount(createCommodityId,uid);
        return count;
    }

    //购物车数据新增
    @RequestMapping("AddShoppingTrolley/{commodityName}/{uid}")
    public Integer AddShoppingTrolley(@PathVariable("commodityName") String commodityName, HttpServletRequest request,@PathVariable("uid") Integer uid){
        Integer createCommodityId = _shopping_car_service.findCommodityById(commodityName);
        ShoppingTrolleyPojo shoppingTrolleyPojo = new ShoppingTrolleyPojo();
        shoppingTrolleyPojo.setCreateUserId(uid);
        shoppingTrolleyPojo.setCreateCommodityId(createCommodityId);
        Integer add = _shopping_car_service.addShoppingTrolley(shoppingTrolleyPojo);
        return add;
    }
}
